$(function() {
$('#loginBox a').on('click',function () {
$('#loginBox').hide();
$('#registerBox').show();

})
$('#registerBox a').on('click',function () {
    $('#registerBox').hide();
    $('#loginBox').show();
    
    })
    let form =layui.form
    form.verify({
        pwd:[
            /^[\S]{6,15}$/,
            "密码长度为6-15个字符,不能包含空格"
        ],
        repwd: function (value) {
            let pwd= $('#registerBox [name=password]').val()
           if(value!==pwd){
               return'密码不一致';
           }
            
        }
    })
    let layer =layui.layer
    $('#form-reg').on('submit',function (e) {
    e.preventDefault()
    axios({
        method:'POST',
        url:'/api/reguser',
        data:$('#form-reg').serialize()
        }).then((res) => {
            if(res.data.status!==0){
                // return alert(res.data.message)
                return layer.msg(res.data.message)
            }
            // alert(res.data.message)
            layer.msg('恭喜注册成功')
            $('#form-reg')[0].reset()
            $('#registerBox a').click()

        })
    })
    $('#form-login').on('submit',function (e) {
        e.preventDefault()
        axios({
            method:'POST',
            url:'/api/login',
            data:$('#form-login').serialize()
        }).then((res)=>{
            let obj=res.data
            if(obj.status!=0){
                return layer.msg(obj.message)
            }
            layer.msg('恭喜登录成功');
            location.href='/index.html';
         localStorage.setItem('token',obj.token)
        })
    })
})